package com.colud.client.test;

import com.colud.client.util.JT809Constants;
import com.colud.client.vo.Message;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.lang.model.element.VariableElement;
import java.nio.charset.Charset;

import static com.colud.client.util.ClientUtils.getBytesWithLengthAfter;

/**
 * @author mfy
 * @date 2021/10/25
 */
@Component
public class JT809ACKManager {

    private static JT809Manager jt809Manager = new JT809Manager();

    public static void ackSend(Message msg) {

        if (msg.getMsgId() == JT809Constants.DOWN_PLATFORM_MSG) {

            //平台查岗请求应答
            if (msg.getMsgId() == JT809Constants.DOWN_PLATFORM_MSG_POST_QUERY_REQ) {
                Message message = new Message(JT809Constants.UP_PLATFORM_MSG);
                ChannelBuffer msgBody = msg.getMsgBody();
                msgBody.skipBytes(6);
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(2 + 4 + 4 + 4 + 20);
                bodyBuffer.writeShort(JT809Constants.UP_PLATFORM_MSG_POST_QUERY_ACK);//2
                bodyBuffer.writeInt(4 + 4 + 20);//应答内容暂定 20
                bodyBuffer.writeInt(msgBody.readInt());//4  消息id
                bodyBuffer.writeInt(20);//4
                bodyBuffer.writeBytes(getBytesWithLengthAfter(20, "平台查岗应答".getBytes(Charset.forName("GBK"))));//20
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }

            //下发平台报文应答
            if (msg.getMsgId() == JT809Constants.DOWN_PLATFORM_MSG_INFO_REQ) {
                Message message = new Message(JT809Constants.UP_PLATFORM_MSG);
                ChannelBuffer msgBody = msg.getMsgBody();
                msgBody.skipBytes(6);
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(2 + 4 + 4);
                bodyBuffer.writeShort(JT809Constants.UP_PLATFORM_MSG_INFO_ACK);//2
                bodyBuffer.writeInt(4);
                bodyBuffer.writeInt(msgBody.readInt());//4
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }
        }

        if (msg.getMsgId() == JT809Constants.DOWN_WARN_MSG) {
            //报警督办应答
            if (msg.getMsgId() == JT809Constants.DOWN_WARN_MSG_URGE_TODO_REQ) {
                Message message = new Message(JT809Constants.UP_WARN_MSG);
                ChannelBuffer msgBody = msg.getMsgBody();
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 4 + 1);
                bodyBuffer.writeBytes(msgBody.readBytes(21));
                bodyBuffer.writeByte(msgBody.readByte());//1
                bodyBuffer.writeShort(JT809Constants.UP_WARN_MSG_URGE_TODO_ACK);//2
                bodyBuffer.writeInt(4 + 1);//4
                msgBody.skipBytes(17);
                bodyBuffer.writeInt(msgBody.readInt());//4 督办id
                bodyBuffer.writeByte(0);//处理中
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }
        }

        if (msg.getMsgId() == JT809Constants.DOWN_EXG_MSG) {
            ChannelBuffer msgBody = msg.getMsgBody();
            ChannelBuffer vehicleNoBuffer = msgBody.readBytes(21);
            byte vehicleColor = msgBody.readByte();
            int dataType = msgBody.readUnsignedShort();
            System.out.println(dataType);
            //启动车辆定位信息交换请求应答
            if (dataType == JT809Constants.DOWN_EXG_MSG_RETURN_STARTUP) {
                Message message = new Message(JT809Constants.UP_EXG_MSG);
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(21 + 1 + 2 + 4);
                bodyBuffer.writeBytes(vehicleNoBuffer);//21
                bodyBuffer.writeByte(vehicleColor);//1
                bodyBuffer.writeShort(JT809Constants.UP_EXG_MSG_RETURN_STARTUP_ACK);//2
                bodyBuffer.writeInt(0);//4
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }

            //关闭车辆定位信息交换请求应答
            if (dataType == JT809Constants.DOWN_EXG_MSG_RETURN_END) {
                Message message = new Message(JT809Constants.DOWN_EXG_MSG);
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(21 + 1 + 2 + 4);
                bodyBuffer.writeBytes(vehicleNoBuffer);//21
                bodyBuffer.writeByte(vehicleColor);//1
                bodyBuffer.writeShort(JT809Constants.UP_EXG_MSG_RETURN_END_ACK);//2
                bodyBuffer.writeInt(0);//4
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }
            //上报车辆驾驶员身份识别信息请求应答
            if (dataType == JT809Constants.DOWN_EXG_MSG_REPORT_DRIVER_INFO) {
                Message message = new Message(JT809Constants.DOWN_EXG_MSG);
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 16 + 20 + 40 + 200);
                bodyBuffer.writeBytes(vehicleNoBuffer);
                bodyBuffer.writeByte(vehicleColor);//1
                bodyBuffer.writeShort(JT809Constants.UP_EXG_MSG_REPORT_DRIVER_INFO_ACK);//2
                bodyBuffer.writeInt(16 + 20 + 40 + 200);//4
                bodyBuffer.writeBytes(getBytesWithLengthAfter(16, "众钢汇".getBytes(Charset.forName("GBK"))));
                bodyBuffer.writeBytes(getBytesWithLengthAfter(20, "472826169609269328".getBytes(Charset.forName("GBK"))));
                bodyBuffer.writeBytes(getBytesWithLengthAfter(40, "123456789123".getBytes(Charset.forName("GBK"))));
                bodyBuffer.writeBytes(getBytesWithLengthAfter(200, "0".getBytes(Charset.forName("GBK"))));
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }
            //上报车辆电子运单请求应答
            if (dataType == JT809Constants.DOWN_EXG_MSG_TAKE_EWAYBILL_REQ) {
                Message message = new Message(JT809Constants.DOWN_EXG_MSG);
                ChannelBuffer bodyBuffer = ChannelBuffers.buffer(21 + 1 + 2 + 4 + 4 + 20);
                bodyBuffer.writeBytes(vehicleNoBuffer);//21
                bodyBuffer.writeByte(vehicleColor);//1
                bodyBuffer.writeShort(JT809Constants.UP_EXG_MSG_TAKE_EWAYBILL_ACK);//2
                bodyBuffer.writeInt(4 + 20);//4
                bodyBuffer.writeInt(20);//运单长度暂定 20
                bodyBuffer.writeBytes(getBytesWithLengthAfter(20, "电子运单内容".getBytes(Charset.forName("GBK"))));//20
                message.setMsgBody(bodyBuffer);
                jt809Manager.reqMsg(message);
            }
        }
    }
}
